/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RecuperacionDeElementos.java
 *
 * Created on 21-oct-2012, 23:55:22
 */
package Test;

import ImagenesDBGad.IO.Objeto;
import Utils.ArchUtils;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import org.apache.batik.dom.svg.SVGOMDocument;
import org.apache.batik.dom.svg.SVGOMPathElement;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
import org.w3c.dom.NodeList;
import org.w3c.dom.svg.SVGDocument;

/**
 * @TODO Se borra nico?
 *
 * @author NiCo
 */
public class RecuperacionDeElementos extends javax.swing.JFrame {

    SVGOMPathElement pathGenerico = null;
    UtilidadesDistancia util = new UtilidadesDistancia();
    SVGDocument fig1;
    SVGDocument fig2;

    /**
     * Creates new form RecuperacionDeElementos
     */
    public RecuperacionDeElementos() {
        initComponents();

        // Set the JSVGCanvas listeners.
        jSVGCanvasPrincipal.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {

            @Override
            public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
            }

            @Override
            public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
            }
        });

        jSVGCanvasPrincipal.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {

            @Override
            public void gvtBuildStarted(GVTTreeBuilderEvent e) {
            }

            @Override
            public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
            }
        });

        jSVGCanvasPrincipal.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {

            @Override
            public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
            }

            @Override
            public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                DefaultComboBoxModel modeloResp = new DefaultComboBoxModel();
                DefaultComboBoxModel modeloInterseccion = new DefaultComboBoxModel();

                SVGOMDocument doc = (SVGOMDocument) jSVGCanvasPrincipal.getSVGDocument();
                NodeList nodos = doc.getRootElement().getElementsByTagName("path");
                for (int i = 0; i < nodos.getLength(); i++) {
                    SVGOMPathElement path = (SVGOMPathElement) nodos.item(i);
                    modeloResp.addElement(path.getAttribute("d"));
                    modeloInterseccion.addElement(path.getAttribute("d"));
                }
                jComboBoxRespuesta.setModel(modeloResp);
                jComboBoxInterseccion.setModel(modeloInterseccion);
            }
        });


        jSVGCanvasRespuesta.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {

            @Override
            public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
            }

            @Override
            public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
            }
        });

        jSVGCanvasRespuesta.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {

            @Override
            public void gvtBuildStarted(GVTTreeBuilderEvent e) {
            }

            @Override
            public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
            }
        });

        jSVGCanvasRespuesta.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {

            @Override
            public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
            }

            @Override
            public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                SVGDocument doc = jSVGCanvasRespuesta.getSVGDocument();
                NodeList nodos = doc.getRootElement().getElementsByTagName("path");
                SVGOMPathElement path = (SVGOMPathElement) nodos.item(0);
                String camino = path.getAttribute("d");
                float alto = path.getBBox().getHeight();
                float ancho = path.getBBox().getWidth();
                float x = path.getBBox().getX();
                float y = path.getBBox().getY();
                float escala = 1;
                if (ancho >= alto) {
                    if (ancho > jSVGCanvasRespuesta.getWidth()) {
                        escala = jSVGCanvasRespuesta.getWidth() / ancho;
                    }
                } else {
                    if (alto > ancho) {
                        if (alto > jSVGCanvasRespuesta.getHeight()) {
                            escala = jSVGCanvasRespuesta.getHeight() / alto;
                        }
                    }
                }
//                crearDoc(camino, escala, x, y);
//                jSVGCanvasRespuesta.setURI(leerDoc().toURI().toString());
                System.out.println("RENDER LISTO!!!");
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jLabel1 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextField2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jSVGCanvasPrincipal = new org.apache.batik.swing.JSVGCanvas();
        jSVGCanvasRespuesta = new org.apache.batik.swing.JSVGCanvas();
        jComboBoxRespuesta = new javax.swing.JComboBox();
        jButtonAbrir = new javax.swing.JButton();
        jButtonCargarElementos = new javax.swing.JButton();
        jButtonLongitudYPuntos = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jComboBoxInterseccion = new javax.swing.JComboBox();
        jButton6 = new javax.swing.JButton();

        jLabel1.setText("Cant. Puntos");

        jButton4.setText("Calcular");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "X", "Y"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Float.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jTextField2.setEditable(false);

        jLabel2.setText("Perimetro figura");

        jButton5.setText("Pto. texto plano");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jFrame1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jFrame1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jFrame1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)))
                .addContainerGap(19, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jFrame1Layout.createSequentialGroup()
                .addContainerGap(57, Short.MAX_VALUE)
                .addComponent(jButton5)
                .addGap(55, 55, 55))
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton5)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jSVGCanvasPrincipalLayout = new javax.swing.GroupLayout(jSVGCanvasPrincipal);
        jSVGCanvasPrincipal.setLayout(jSVGCanvasPrincipalLayout);
        jSVGCanvasPrincipalLayout.setHorizontalGroup(
            jSVGCanvasPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 293, Short.MAX_VALUE)
        );
        jSVGCanvasPrincipalLayout.setVerticalGroup(
            jSVGCanvasPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 308, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jSVGCanvasRespuestaLayout = new javax.swing.GroupLayout(jSVGCanvasRespuesta);
        jSVGCanvasRespuesta.setLayout(jSVGCanvasRespuestaLayout);
        jSVGCanvasRespuestaLayout.setHorizontalGroup(
            jSVGCanvasRespuestaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 303, Short.MAX_VALUE)
        );
        jSVGCanvasRespuestaLayout.setVerticalGroup(
            jSVGCanvasRespuestaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 265, Short.MAX_VALUE)
        );

        jComboBoxRespuesta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxRespuestaActionPerformed(evt);
            }
        });

        jButtonAbrir.setText("Abrir");
        jButtonAbrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAbrirActionPerformed(evt);
            }
        });

        jButtonCargarElementos.setText("Cargar elementos");
        jButtonCargarElementos.setEnabled(false);
        jButtonCargarElementos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCargarElementosActionPerformed(evt);
            }
        });

        jButtonLongitudYPuntos.setText("Longitud y puntos");
        jButtonLongitudYPuntos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLongitudYPuntosActionPerformed(evt);
            }
        });

        jButton1.setText("45º");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("vertical");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("interseccion");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton6.setText("jButton6");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jSVGCanvasPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(38, 38, 38)
                                    .addComponent(jButtonAbrir)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                                    .addComponent(jButtonCargarElementos)
                                    .addGap(127, 127, 127)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jComboBoxRespuesta, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jSVGCanvasRespuesta, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(92, 92, 92))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(16, 16, 16)
                                    .addComponent(jButtonLongitudYPuntos)
                                    .addGap(27, 27, 27)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jButton1)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                                            .addComponent(jButton6)
                                            .addGap(54, 54, 54))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jButton2)
                                            .addContainerGap(166, Short.MAX_VALUE))))))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jButton3)
                            .addGap(191, 191, 191)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jComboBoxInterseccion, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(101, 101, 101))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jComboBoxRespuesta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSVGCanvasRespuesta, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonLongitudYPuntos)
                            .addComponent(jButton1)
                            .addComponent(jButton6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonAbrir)
                            .addComponent(jButtonCargarElementos))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSVGCanvasPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(15, 15, 15)
                .addComponent(jComboBoxInterseccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAbrirActionPerformed
//        JFileChooser fc = new JFileChooser(".");
//        int choice = fc.showOpenDialog(this);
//        if (choice == JFileChooser.APPROVE_OPTION) {
//            File f = fc.getSelectedFile();
//            try {
//                jSVGCanvasPrincipal.setURI(f.toURI().toString());
//            } catch (Exception ex) {
//                ex.printStackTrace();
//            }
//        }
        String url = ArchUtils.seleccionarArchivo();
        jSVGCanvasPrincipal.setURI(url);
    }//GEN-LAST:event_jButtonAbrirActionPerformed

    private void jButtonCargarElementosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCargarElementosActionPerformed
//        DefaultComboBoxModel modelo = new DefaultComboBoxModel();
//        SVGOMDocument doc = (SVGOMDocument) jSVGCanvas1.getSVGDocument();
//        NodeList nodos = doc.getRootElement().getElementsByTagName("path");
//        for (int i = 0; i < nodos.getLength(); i++) {
//            SVGOMPathElement path = (SVGOMPathElement) nodos.item(i);
//            modelo.addElement(path.getAttribute("d"));
//        }
//        jComboBox1.setModel(modelo);
    }//GEN-LAST:event_jButtonCargarElementosActionPerformed

    private void jComboBoxRespuestaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxRespuestaActionPerformed
        crearDocAux(jComboBoxRespuesta.getSelectedItem().toString());
        jSVGCanvasRespuesta.setURI(leerDoc().toURI().toString());
    }//GEN-LAST:event_jComboBoxRespuestaActionPerformed

    private void jButtonLongitudYPuntosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLongitudYPuntosActionPerformed
//        SVGDocument doc = jSVGCanvas2.getSVGDocument();
//        NodeList nodos = doc.getRootElement().getElementsByTagName("path");
//        SVGOMPathElement path = (SVGOMPathElement) nodos.item(0);
//        String camino = path.getAttribute("d");
//        float alto = path.getBBox().getHeight();
//        float ancho = path.getBBox().getWidth();
//        float x = path.getBBox().getX();
//        float y = path.getBBox().getY();
//        String[] caminoSeparado = camino.split(" ");
//        String caminoJunto = "";
//        float escala = 1;
////        for (int i = 0; i < caminoSeparado.length; i++) {
////            if (i == 0) {
////                String aux = caminoSeparado[i].substring(0, 1);
////                caminoJunto = caminoJunto + aux + String.valueOf(x) + " ";
////            }
////            if (i == 1) {
////                caminoJunto = caminoJunto + String.valueOf(y) + " ";
////            }
////            if (i > 1) {
////                caminoJunto = caminoJunto + caminoSeparado[i] + " ";
////            }
////        }
//        if (ancho >= alto) {
//            if (ancho > jSVGCanvas2.getWidth()) {
//                escala = jSVGCanvas2.getWidth() / ancho;
//            }
//        } else {
//            if (alto > ancho) {
//                if (alto > jSVGCanvas2.getHeight()) {
//                    escala = jSVGCanvas2.getHeight() / alto;
//                }
//            }
//        }
//        crearDoc(camino, escala, x, y);
//        jSVGCanvas2.setURI(leerDoc().toURI().toString());

        jFrame1.setVisible(true);
        jFrame1.setSize(231, 349);
        SVGDocument doc = jSVGCanvasRespuesta.getSVGDocument();
        SVGOMPathElement path = (SVGOMPathElement) doc.getRootElement().getElementById("camino");
        jTextField2.setText(String.valueOf(path.getTotalLength()));
    }//GEN-LAST:event_jButtonLongitudYPuntosActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        SVGDocument doc = jSVGCanvasRespuesta.getSVGDocument();
        SVGOMPathElement path = (SVGOMPathElement) doc.getRootElement().getElementById("camino");
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        int puntos = Integer.parseInt(jTextField1.getText());
        float acumulado = 0;
        float incremento = path.getTotalLength() / puntos;
        borrarTabla(modelo);
        for (int i = 0; i < puntos; i++) {
            Object[] rowData = new Object[2];
            rowData[0] = path.getPointAtLength(acumulado).getX();
            rowData[1] = path.getPointAtLength(acumulado).getY();
            acumulado = acumulado + incremento;
            modelo.addRow(rowData);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ArrayList<Object[]> puntos = new ArrayList<Object[]>();
        SVGDocument doc = jSVGCanvasRespuesta.getSVGDocument();
        SVGOMPathElement path = (SVGOMPathElement) doc.getRootElement().getElementById("camino");
        float acumulado = 0;
        float incremento = path.getTotalLength() / 100;
        for (int i = 0; i < 100; i++) {
            Object[] rowData = new Object[2];
            rowData[0] = path.getPointAtLength(acumulado).getX();
            rowData[1] = path.getPointAtLength(acumulado).getY();
            acumulado = acumulado + incremento;
            puntos.add(rowData);
        }

        float xc = path.getBBox().getX() + path.getBBox().getWidth() / 2;
        float yc = path.getBBox().getY() + path.getBBox().getHeight() / 2;
        String pathN = "M";
        for (Iterator<Object[]> it = puntos.iterator(); it.hasNext();) {
            Object[] obj = it.next();
            double xn = xc + (Float.parseFloat(obj[0].toString()) - xc) * Math.cos(45) - (Float.parseFloat(obj[1].toString()) - yc) * Math.sin(45);
            double yn = yc + (Float.parseFloat(obj[0].toString()) - xc) * Math.sin(45) + (Float.parseFloat(obj[1].toString()) - yc) * Math.cos(45);
            pathN = pathN + " " + String.valueOf(xn) + " " + String.valueOf(yn);
        }
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) jComboBoxRespuesta.getModel();
        modelo.addElement(pathN);


    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        ArrayList<Object[]> puntos = new ArrayList<Object[]>();
        SVGDocument doc = jSVGCanvasRespuesta.getSVGDocument();
        SVGOMPathElement path = (SVGOMPathElement) doc.getRootElement().getElementById("camino");
        float acumulado = 0;
        float incremento = path.getTotalLength() / 100;
        for (int i = 0; i < 100; i++) {
            Object[] rowData = new Object[2];
            rowData[0] = path.getPointAtLength(acumulado).getX();
            rowData[1] = path.getPointAtLength(acumulado).getY();
            acumulado = acumulado + incremento;
            puntos.add(rowData);
        }

        float relacionV = path.getBBox().getHeight() / path.getBBox().getWidth();
        float xmv = path.getBBox().getX();
        float ymv = path.getBBox().getY();
        float xMv = path.getBBox().getX() + path.getBBox().getWidth();
        float yMv = path.getBBox().getY() + path.getBBox().getHeight();
        float xcv = path.getBBox().getX() + path.getBBox().getWidth() / 2;
        float ycv = path.getBBox().getY() + path.getBBox().getHeight() / 2;
        String caminoV = path.getAttribute("d");

        ArrayList<Object[]> puntosN = new ArrayList<Object[]>();
        float relacionN = 0;
        float xmn = Float.MAX_VALUE;
        float ymn = Float.MAX_VALUE;
        float xMn = Float.MIN_VALUE;
        float yMn = Float.MIN_VALUE;
        float xcn;
        float ycn;
        String caminoN = "M";

        int grados = 5;
        while (relacionV > relacionN || grados <= 180) {
            puntosN.clear();
            relacionN = 0;
            xmn = Float.MAX_VALUE;
            ymn = Float.MAX_VALUE;
            xMn = Float.MIN_VALUE;
            yMn = Float.MIN_VALUE;
            xcn = 0;
            ycn = 0;
            for (Iterator<Object[]> it = puntos.iterator(); it.hasNext();) {
                Object[] obj = it.next();
                Object[] punto = new Object[2];
                punto[0] = xcv + (Float.parseFloat(obj[0].toString()) - xcv) * Math.cos(grados) - (Float.parseFloat(obj[1].toString()) - ycv) * Math.sin(grados);
                punto[1] = ycv + (Float.parseFloat(obj[0].toString()) - xcv) * Math.sin(grados) + (Float.parseFloat(obj[1].toString()) - ycv) * Math.cos(grados);
                puntosN.add(punto);

                if (Float.parseFloat(punto[0].toString()) < xmn) {
                    xmn = Float.parseFloat(punto[0].toString());
                }
                if (Float.parseFloat(punto[0].toString()) > xMn) {
                    xMn = Float.parseFloat(punto[0].toString());
                }
                if (Float.parseFloat(punto[1].toString()) < ymn) {
                    ymn = Float.parseFloat(punto[1].toString());
                }
                if (Float.parseFloat(punto[1].toString()) > yMn) {
                    yMn = Float.parseFloat(punto[1].toString());
                }
            }
            xcn = xmn + (xMn - xmn) / 2;
            ycn = ymn + (yMn - ymn) / 2;
            relacionN = (yMn - ymn) / (xMn - xmn);

            if (relacionN > relacionV) {
                relacionV = relacionN;
                xmv = xmn;
                xMv = xMn;
                ymv = ymn;
                yMv = yMn;
                xcv = xcn;
                xcv = xcn;
                puntos.clear();
                puntos.addAll(puntosN);
            }
            grados += 5;
        }

        for (Iterator<Object[]> it = puntos.iterator(); it.hasNext();) {
            Object[] obj = it.next();
            caminoN = caminoN + " " + String.valueOf(obj[0]) + " " + String.valueOf(obj[1]);
        }
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) jComboBoxRespuesta.getModel();
        modelo.addElement(caminoN);

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        SVGOMPathElement path = (SVGOMPathElement) fig1.getRootElement().getElementById("camino");
        Objeto figura1 = util.estandarizarImagenes(path);

        fig2 = jSVGCanvasRespuesta.getSVGDocument();
        path = (SVGOMPathElement) fig2.getRootElement().getElementById("camino");

        Objeto figura2 = util.estandarizarImagenes(path);

//       double distancia = util.distanciaEntreObjetos(figura1, figura2);

//        JOptionPane.showMessageDialog(this, distancia);

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        String ptoX = "";
        String ptoY = "";
        for (int i = 0; i < modelo.getRowCount(); i++) {
            Object x = modelo.getValueAt(i, 0);
            Object y = modelo.getValueAt(i, 1);
            ptoX = ptoX + " " + x;
            ptoY = ptoY + " " + y;

//            System.out.println("p2.AddVertex(" + Math.round(Double.parseDouble(x.toString())) + ", " + Math.round(Double.parseDouble(y.toString()) + 300) + ");");
        }
        System.out.println(ptoX);
        System.out.println(ptoY);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        fig1 = jSVGCanvasRespuesta.getSVGDocument();
    }//GEN-LAST:event_jButton6ActionPerformed

    private void borrarTabla(DefaultTableModel modelo) {
        for (int i = 0; i < modelo.getRowCount(); i++) {
            modelo.removeRow(0);
        }
    }

    public void crearDocAux(String path) {
        try {
            String homeUsuario = System.getProperty("user.home");
//            File f = new File(homeUsuario + "/hola.svg");
            File f = new File("./src/Imagenes/SVGAuxiliar.svg");
//            File f = new File("d:/hola.svg");
            if (f.exists()) {
                f.delete();
            }
            FileWriter fichero = new FileWriter(f);
            PrintWriter pw = new PrintWriter(fichero);

            pw.println("<?xml version='1.0' standalone='no'?>");
            pw.println("<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 20010904//EN' 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>");
            pw.println("<svg  version = '1.0' xmlns = 'http://www.w3.org/2000/svg'>");
            pw.println("<path id ='camino' d='" + path + "'/>");
            pw.println("</svg>");

            pw.close();
            fichero.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void crearDoc(String path, float escala, float x, float y) {
        try {
            String homeUsuario = System.getProperty("user.home");
//            File f = new File(homeUsuario + "/hola.svg");
            File f = new File("./src/Imagenes/SVGAuxiliar.svg");
//            File f = new File("d:/hola.svg");

            if (f.exists()) {
                f.delete();
            }
            FileWriter fichero = new FileWriter(f);
            PrintWriter pw = new PrintWriter(fichero);

            pw.println("<?xml version='1.0' standalone='no'?>");
            pw.println("<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 20010904//EN' 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>");
            pw.println("<svg  version = '1.0' xmlns = 'http://www.w3.org/2000/svg'>");
            pw.println("<g transform='scale(" + escala + ") translate(" + x * -1 + " " + y * -1 + ")'>");
            pw.println("<path id ='camino' d='" + path + "'/>");
            pw.println("</g>");
            pw.println("</svg>");

            pw.close();
            fichero.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public File leerDoc() {
        File doc = null;
        try {
            String homeUsuario = System.getProperty("user.home");
//            doc = new File(homeUsuario + "/hola.svg");
            doc = new File("./src/Imagenes/SVGAuxiliar.svg");
//            doc = new File("d:/hola.svg");

        } catch (Exception ex) {
        }
        return doc;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RecuperacionDeElementos().setVisible(true);
            }
        });


    }

    private void interseccion(ArrayList<Object[]> puntosFig1, ArrayList<Object[]> puntosFig2) {


        String caminoN = "M";
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) jComboBoxRespuesta.getModel();
        modelo.addElement(caminoN);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButtonAbrir;
    private javax.swing.JButton jButtonCargarElementos;
    private javax.swing.JButton jButtonLongitudYPuntos;
    private javax.swing.JComboBox jComboBoxInterseccion;
    private javax.swing.JComboBox jComboBoxRespuesta;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private org.apache.batik.swing.JSVGCanvas jSVGCanvasPrincipal;
    private org.apache.batik.swing.JSVGCanvas jSVGCanvasRespuesta;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
